package com.hoodbrains.insideloc.locator;

import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

import android.location.Location;
import android.util.Log;

public class SuperMarketPOI {
	
	String osmId;
	String name = "Supermarket";
	double latitude;
	double longitude;
	double distance;
	
	public String getOsmId() {
		return osmId;
	}

	public void setOsmId(String osmId) {
		this.osmId = osmId;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public double getLatitude() {
		return latitude;
	}

	public void setLatitude(double latitude) {
		this.latitude = latitude;
	}

	public double getLongitude() {
		return longitude;
	}

	public void setLongitude(double longitude) {
		this.longitude = longitude;
	}

	public double getDistance(){
		return distance;
	}
	
	public void computeDistance(Location loc){
		distance = Locator.distanceBetween(loc, latitude, longitude);
	}
	
	public String toString() {
		return name+" ~"+(int)distance+" m";
	}
	
	public static SuperMarketPOI deserialize(Element elem) {
		SuperMarketPOI smPOI = new SuperMarketPOI();
		
		smPOI.latitude = Double.parseDouble(elem.getAttribute("lat"));
		smPOI.longitude = Double.parseDouble(elem.getAttribute("lon"));
		smPOI.osmId = elem.getAttribute("id");
		
		NodeList tags = elem.getElementsByTagName("tag");
		for (int i=0; i<tags.getLength(); i++) {
			Element tagElem = (Element)tags.item(i);
			
			if (tagElem.hasAttribute("k") && "name".equals(tagElem.getAttribute("k"))) {
				smPOI.name = tagElem.getAttribute("v");
				break;
			}
		}
		
		Log.e("SuperMarketPOI", smPOI.toString());
		return smPOI;
		
	}
}
